package element.synt;

import ast.AstItem;
import ast.Expression;
import ast.LValStatement;
import ast.Statement;
import element.base.Synt;
import element.base.WordType;

public class SimpleStmt extends Synt {
    @Override
    public AstItem toAst() {
        if (getNodes().isEmpty()) {
            return null;
        } else if (getNodes().getFirst().isSynt(Exp.class)) {
            Expression expression = (Expression) ((Exp) getNodes().getFirst()).toAst();
            LValStatement statement = new LValStatement();
            statement.type = Statement.StatementType.EXP_STMT;
            statement.RVal = expression;
            return statement;
        } else {
            if (getNodes().get(1).isWord(WordType.ADD) &&
                    getNodes().get(2).isWord(WordType.ADD)) {
                LValStatement statement = new LValStatement();
                statement.type = Statement.StatementType.SELF_ADD_STMT;
                LVal lVal = (LVal) getNodes().getFirst();
                statement.LVal = lVal.toAst();
                return statement;
            } else if (getNodes().get(1).isWord(WordType.MINU) &&
                    getNodes().get(2).isWord(WordType.MINU)) {
                LValStatement statement = new LValStatement();
                statement.type = Statement.StatementType.SELF_MINU_STMT;
                LVal lVal = (LVal) getNodes().getFirst();
                statement.LVal = lVal.toAst();
                return statement;
            } else {
                LValStatement statement = new LValStatement();
                statement.type = Statement.StatementType.ASSIGN_STMT;
                LVal lVal = (LVal) getNodes().getFirst();
                statement.LVal = lVal.toAst();

                Assign assign = (Assign) getNodes().get(1);
                if (assign.findFirst().isWord(WordType.ASSIGN)) {
                    statement.assignType = LValStatement.AssignType.NORMAL;
                } else if (assign.findFirst().isWord(WordType.ADDASSIGN)) {
                    statement.assignType = LValStatement.AssignType.ADD;
                } else if (assign.findFirst().isWord(WordType.MINUASSIGN)) {
                    statement.assignType = LValStatement.AssignType.MINU;
                } else if (assign.findFirst().isWord(WordType.MULTASSIGN)) {
                    statement.assignType = LValStatement.AssignType.MULT;
                } else if (assign.findFirst().isWord(WordType.DIVASSIGN)) {
                    statement.assignType = LValStatement.AssignType.DIV;
                } else if (assign.findFirst().isWord(WordType.MODASSIGN)) {
                    statement.assignType = LValStatement.AssignType.MOD;
                }

                Expression expression = (Expression) ((Exp) getNodes().get(2)).toAst();
                statement.RVal = expression;
                return statement;
            }
        }
    }
}
